<?php
//微信用户相关

namespace App\Http\Controllers;

use App\Models\OperateModel;
use App\Repositories\StrategyRepository;
use Illuminate\Http\Request;
use App\Repositories\OperateRepository;
use Illuminate\Support\Facades\DB;

class OperateController extends BaseController
{

    /**
     * @param Request $request
     * @param OperateRepository $operateRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function list (Request $request, OperateRepository $operateRepository)
    {
        $params = $request->all();
        $list = @$operateRepository->getList($params, ['note:id,operate_id']);
//        var_dump($list);
        $listData = @$operateRepository->addUnFinishDealData($list['list']);
        $list['list'] = $listData['list'];
        $list['deal_count'] = @$operateRepository->getListDealCount($listData['list_ids']);
        return $this->success($list);
    }

    /**
     * @param Request $request
     * @param OperateRepository $operateRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function save (Request $request, OperateRepository $operateRepository)
    {
        $allParams = $request->all();
        $rst = @$operateRepository->save($allParams);
        if ($rst) {
            return $this->success([]);
        }
        return $this->error(405, '保存失败，请稍后再试');
    }

    /**
     * @param Request $request
     * @param OperateRepository $operateRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getOperateInfo (Request $request, OperateRepository $operateRepository)
    {
        $id = $request->get('id');
        $data = @$operateRepository->getOperateInfo($id);
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * 获取K线数据
     */
    public function getOperateKline (OperateRepository $operateRepository)
    {
        $data = @$operateRepository->getOperateKline();
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param OperateRepository $operateRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function getFollowInfo (Request $request, OperateRepository $operateRepository)
    {
        $id = $request->get('id');
        $data = @$operateRepository->getFollowInfo($id);
        if ($data) {
            return $this->success($data);
        } else {
            return $this->error(401, '信息获取失败，请稍后再试');
        }
    }

    /**
     * @param Request $request
     * @param OperateRepository $operateRepository
     * @return \Illuminate\Http\JsonResponse
     */
    public function finish (Request $request, OperateRepository $operateRepository, StrategyRepository $strategyRepository)
    {
        DB::beginTransaction();
        $actualEarnings = $request->get('actual_earnings');
        $operateId = $request->get('operate_id');
        $strategyId = $request->get('strategy_id');
        $data = @$operateRepository->finish($operateId, $actualEarnings);
        if ($data && $data['errcode'] == 0) {
            $rst = @$strategyRepository->updateEarningRate($strategyId);
            if ($rst) {
                DB::commit();
                return $this->success($data);
            } else {
                DB::rollBack();
                return $this->error(401, '操作失败，请稍后再试');
            }
        } else {
            return $this->error(401, $data['errmsg']);
        }
    }

    public function setArchive (Request $request)
    {
        $id = $request->get('id');
        $rst = @OperateModel::where('id', $id)
            ->update([
                'archived' => OperateModel::IS_ARCHIVED
            ]);
        if ($rst) {
            return $this->success();
        } else {
            return $this->error(401, '设置失败，请稍后再试');
        }
    }
}
